		
	
capture cd D:\Dropbox\book_welfare\replication\



	
*********************************
* GR by types
*********************************

* make Nfcap_gr.dta file  :

use data\books_year.dta, clear 

		keep if year==2016
		keep if pubyr>=1960 & pubyr<=2016 
		replace fbook = 0 if fbook==. 
		
		collapse (sum) q (count) n=q, by(pubyr fbook)
		keep if fbook~=.
		reshape wide q n , i(pubyr) j(fbook)
		gen nratio = n1/n0 
		twoway (line nratio pubyr)
		su nratio if pubyr>=1960 & pubyr<1970 
		local a=r(mean)
		gen Nfcap = `a'*n0
		
		egen Q1 = sum(q1) if pubyr<1970 
		egen Q0 = sum(q0) if pubyr<1970 
		
		gen sf = Q1/(Q1+Q0)
		egen sftarget = max(sf)
		keep pubyr Nfcap sftarget 
		
save data\Nfcap_gr.dta, replace 


	


use  data\books_year_types.dta, clear 
		keep if year==2016
		keep if pubyr>=1960 & pubyr<=2016 
		mvencode fbook, mv(0) o 
		merge m:1 pubyr using clean_room\Nfcap_gr.dta

	
		gen double M =  10.00e+07
		gen double s = q/M 
		egen Q=sum(q)
		gen double s0 = (M-Q)/M
	

	
		rename qdromance    qRO 
		rename qdchildren   qCH
		rename qdcomics     qCO
		rename qdfantasy    qFA
		rename qdfiction    qFI 
		rename qdhistory    qHI
		rename qdmystery    qMY
		rename qdnonfiction qNF 
		rename qdpoetry     qPO 
		rename qdyoungadult qYA  




		* by apparent gender of consumer 
		egen QF=sum(qf)
		egen QM=sum(qm)



		gen   MF = (QF/(QM+QF))*M
		gen double sf = qf/MF 
		gen double s0f = (MF-QF)/MF

		gen   MM = (QM/(QM+QF))*M
		gen double sm = qm/MM 
		gen double s0m = (MM-QM)/MM

	
	* by genre-dependence of users

	
		local type "RO CH CO FA FI HI MY NF PO YA"	
			foreach x of local type {
			egen Q`x' = sum(q`x')
			gen Qn`x' = Q-Q`x' 
			
			gen   M`x' = (Q`x'/(Qn`x'+Q`x'))*M
			gen double s`x' = q`x'/M`x' 
			gen double s0`x' = (M`x'-Q`x')/M`x'

			gen   Mn`x' = (Qn`x'/(Qn`x'+Q`x'))*M
			gen double sn`x' = (q-q`x')/Mn`x' 
			gen double s0n`x' = (Mn`x'-Qn`x')/Mn`x'
		}
			
			egen xA= sum(q) if pubyr<year, by(author_id)
			egen A=max(xA), by(author_id)
			
			mvencode A, mv(0) 
			
			gen lA = ln(A+1)
			gen dmiss=A==0 

			gen dfemale=fbook 
	
	

	keep M q Q dfemale s0 s Nfcap pubyr s sf sm s0f s0m MM MF qf qm QM QF ///
	q?? s?? sn?? M?? Mn?? Q??  Qn?? s0?? s0n?? genre lA dmiss fbook  
	keep if q~=. 
	gen p = 10 

	gen pyear= pubyr  
	rename s sj 

save data\X_type.dta, replace 
	
	

*********************************************
*********************************************
clear 
set obs 1 
gen v1=.
save clean_room\collect_type.dta, replace
*********************************************
*********************************************

 foreach sigma in  .373   {

 use data\X_type.dta, clear 
		egen gno =group(genre)
		 drop M 

		gen sigma = `sigma'
		
		gen double delta = log(sj) - log(s0) - sigma*log(q/Q)
		gen double deltam = ln(sm) - ln(s0m) - sigma*ln(sm/(1-s0m))
		gen double deltaf = ln(sf) - ln(s0f) - sigma*ln(sf/(1-s0f))
	
local type "RO CH CO FA FI HI MY NF PO YA"	
	foreach x of local type {
			gen delta`x' = ln(s`x') - ln(s0`x') - sigma*ln(s`x'/(1-s0`x'))
			gen deltan`x' = ln(sn`x') - ln(s0n`x') - sigma*ln(sn`x'/(1-s0n`x'))
		}
	
		* predictive model: expected quality based on X's	
		 reg delta  lA i.gno dmiss fbook 
		 predict dh 
		 gen res=delta-dh 
		 gen res_sq = res^2 
		 gen sighat = res_sq^.5 
		 
		 
		 * parameterize 
		 
		 gsort fbook -dh 
		 bysort fbook: gen count =_n
		 bysort fbook: gen COUNT =_N
		 
		
		regress dh count if count/COUNT>.9 & fbook==0
		estimates save data\dh, replace 
		gen lsighat = ln(sighat)
		regress lsighat count if count/COUNT>.9 & fbook==0
		estimates save data\sighat, replace 
		
		
		* make a count variable overall
		drop count COUNT
		 gsort -dh 
		 gen count=_n 

		
		
		gsort fbook pubyr  -dh 
		bysort fbook pubyr: gen rank=_n 

		gen drop = (fbook==1 & rank > Nfcap)
		gsort -dh 
		
		* initial cs overall, rev - overall and for M and W 
		preserve 
			gen double M = 10.00e+07
			gen expdelta = exp(delta/(1-sigma))
			egen D=sum(expdelta) 
			gen q0 = M*(expdelta/D)*(D^(1-sigma))/(1+D^(1-sigma))
			gen double CS= log(1 + D^(1-sigma))
			su CS
			local cs0=r(mean)
			
			su q0 if _n/_N>.95 
			local old = r(mean)
			
			gen s_in = [D^(1-sigma)]/[1+D^(1-sigma)]
			gen rev=p*q0
			egen REV = sum(rev)
			gen revf=p*q0*dfemale
			egen REVF = sum(revf)
			gen revm=p*q0*(1-dfemale)
			egen REVM = sum(revm)
			
			su REV
			local rev0=r(mean) 
			su REVF
			local rev0f=r(mean) 
			su REVM
			local rev0m=r(mean) 
			
		restore 
		
		* initial cs male 
		preserve 
			gen double M = 10.00e+07
			gen expdeltam = exp(deltam/(1-sigma))
			egen Dm=sum(expdeltam) 
			gen q0m = M*(expdeltam/Dm)*(Dm^(1-sigma))/(1+Dm^(1-sigma))
			gen double CSm= log(1 + Dm^(1-sigma))
			su CSm
			local cs0m=r(mean)
		restore 
		
		* initial cs female  
		preserve 
			gen double M = 10.00e+07
			gen expdeltaf = exp(deltaf/(1-sigma))
			egen Df=sum(expdeltaf) 
			gen q0f = M*(expdeltaf/Df)*(Df^(1-sigma))/(1+Df^(1-sigma))
			gen double CSf= log(1 + Df^(1-sigma))
			su CSf
			local cs0f=r(mean)
		restore 
		
	local type "RO CH CO FA FI HI MY NF PO YA"	
	foreach x of local type {
		
			

			gen exp_delta`x' = exp(delta`x'/(1-sigma))
			egen D`x' = sum(exp_delta`x')
			gen D`x'_sigma = D`x'^(1-sigma)
			gen CS`x' = ln(1+D`x'_sigma)

			gen exp_deltan`x' = exp(deltan`x'/(1-sigma))
			egen Dn`x' = sum(exp_deltan`x')
			gen Dn`x'_sigma = Dn`x'^(1-sigma)
			gen CSn`x' = ln(1+Dn`x'_sigma)
			
		}			
			
	
		*create a local variable (N) showing the status quo number of male-authored books in the choice set 
		su count if fbook==0 
		local N = r(N)

		
		
		
		***********************************************************
		* create additional male products based on relationship
		* between delta hat and entry order 
		***********************************************************
	
		preserve 
			clear 
			set obs 2000000
			gen count=_n 
			
			estimates use clean_room\dh
				local a = _b[_cons]
				local b = _b[count]
			estimates use clean_room\sighat 
				local c = _b[_cons]
				local d = _b[count]

			
			
			gen xdelta = `a' + `b'*count 
			set seed 99 
			drawnorm xepsilon 
			
			gen epsilon = (exp(`c' + `d'*count))*xepsilon 
			
			gen delta =xdelta + epsilon 
			
			keep if _n>`N'
 
			keep delta count 
			gen extra=1
			
			save data\A.dta, replace
		restore 
	

	
		local T=_N
		
		append using data\A.dta
		
		save data\C.dta, replace 
		
		
		*********** all: this module calculates the status quo entry threshold, `old' 
		
		use data\C.dta, clear 

		 replace sigma=`sigma' if sigma==. 
		
		 drop count 
		 gen count=_n 
		
		
			capture drop M 
			capture drop expdelta D q0 
			
			gen double M = 10.00e+07
			gen expdelta = exp(delta/(1-sigma))
			gen D=sum(expdelta) 
			gen q0 = M*(expdelta/D)*(D^(1-sigma))/(1+D^(1-sigma))
			gen double CS= log(1 + D^(1-sigma))

			
			su q0
			su q0 if _n>`T'-5001 & _n<`T' 
			local old = r(mean)

			

			*******************************************************************
			*********** remove the women, and add endogenous male products  
			*******************************************************************
		use data\C.dta, clear
		drop if drop==1 

			replace sigma=`sigma' if sigma==. 
			
			drop count 
			gen count=_n 
			

			* calculate marginal q for varying N 
			capture drop M 
			capture drop expdelta D  
			gen double M = 10.00e+07
			gen expdelta = exp(delta/(1-sigma))
			gen D=sum(expdelta) 
			gen qmN = M*(expdelta/D)*(D^(1-sigma))/(1+D^(1-sigma))
			
			  
			capture drop q0 
			
			* figure out how many additional male products by equating expected marginal q with the old entry threshold 
			preserve 
				reg qmN count if count>500000
				gen nstar =  (`old' - _b[_cons])/_b[count]
				su nstar
				local nstar=r(mean)
			restore 
			
			
			
			* now, just keep the products entering in the counterfactual
			
			gen double CS= log(1 + D^(1-sigma))
			su CS if _n==int(`nstar')
			local csw = r(mean)
			drop D expdelta 

			drop if _n>int(`nstar')
			
			
			* calculate counterfactual CS 
			gen expdelta = exp(delta/(1-sigma))
			egen D=sum(expdelta) 
			gen q1 = M*(expdelta/D)*(D^(1-sigma))/(1+D^(1-sigma))
			gen double CSW= log(1 + D^(1-sigma))

			
			gen expdeltam = exp(deltam/(1-sigma))
			egen Dm=sum(expdeltam) 
			gen q1m = M*(expdeltam/Dm)*(Dm^(1-sigma))/(1+Dm^(1-sigma))
			gen double CSWm= log(1 + Dm^(1-sigma))
		

			gen expdeltaf = exp(deltaf/(1-sigma))
			egen Df=sum(expdeltaf) 
			gen q1f = M*(expdeltaf/Df)*(Df^(1-sigma))/(1+Df^(1-sigma))
			gen double CSWf= log(1 + Df^(1-sigma))
			
		
local type "RO CH CO FA FI HI MY NF PO YA"	
	foreach x of local type {
		
			reg delta`x' delta 
			predict pp 
			gen delta`x'1 = delta`x'
			replace delta`x'1 = pp if delta`x'1==. & extra==1
			
			reg deltan`x' delta 
			predict pn 
			gen deltan`x'1 = deltan`x'
			replace deltan`x'1 = pn if deltan`x'1==. & extra==1
			
			drop pp pn 	

			
			replace exp_delta`x' = exp(delta`x'1/(1-sigma))
			replace exp_deltan`x' = exp(deltan`x'1/(1-sigma))
			
			egen D1`x' = sum(exp_delta`x')
			gen D1`x'_sigma = D1`x'^(1-sigma)
			gen CS`x'1 = ln(1+D1`x'_sigma)

			egen Dn1`x' = sum(exp_deltan`x')
			gen Dn1`x'_sigma = Dn1`x'^(1-sigma)
			gen CSn`x'1 = ln(1+Dn1`x'_sigma)
			
		}			
			
			
			
			gen rev1 = p*q1 
			egen REV1 = sum(rev1)
			gen revm1 = p*q1*(1-dfemale) 
			egen REVM1 = sum(revm1)
			gen revf1 = p*q1*dfemale 
			egen REVF1 = sum(revf1)
			
			
			keep count CS* sigma REV*
			


			 drop CS 
		
		gen CS=`cs0'
		gen CSm = `cs0m'
		gen CSf = `cs0f'
		gen CSW_alt = `csw'
		
		gen REV0 = `rev0'
		gen REVm = `rev0m'
		gen REVf = `rev0f'
		
		
		keep CS*   CSWf CSWm sigma 
		keep if _n==1 
		append using data\collect_type.dta
		save data\collect_type.dta, replace 
			
	}
	
				
	
capture cd D:\Dropbox\book_welfare\replication



		
use data\collect_type.dta, clear 
	
	
		local type "RO CH CO FA FI HI MY NF PO YA"	
			foreach x of local type {
				gen cs`x'_ch=100*CS`x'/CS`x'1 -100
				gen csn`x'_ch=100*CSn`x'/CSn`x'1 -100
			}
			
			gen count=_n 
			keep count cs* 
			reshape long cs, i(count) j(var) s	
			
			split var, parse("n" "cs" )
			
			gen type="romance" if index(var1,"RO") + index(var2,"RO")>0 
			replace type="children" if index(var1,"CH") + index(var2,"CH")>0 
			replace type="comics" if index(var1,"CO") + index(var2,"CO")>0 
			replace type="fantasy" if index(var1,"FA") + index(var2,"FA")>0 
			replace type="fiction" if index(var1,"FI") + index(var2,"FI")>0 
			replace type="history" if index(var1,"HI") + index(var2,"HI")>0 
			replace type="mystery" if index(var1,"MY") + index(var2,"MY")>0 
			replace type="nonfiction" if index(var1,"NF") + index(var2,"NF")>0 
			replace type="poetry" if index(var1,"PO") + index(var2,"PO")>0 
			replace type="young adult" if index(var1,"YA") + index(var2,"YA")>0 
			
			gen low=index(var,"n")>0 
			
			gsort low -cs 
			
			egen tno= group(type)
			

			keep type cs low
			keep if cs~=.
			drop if type==""
			reshape wide cs, i(type) j(low)
			gen dif= cs1 -cs0 
			gsort -dif 
			gen order=_n 
			br order type 
			
	twoway (scatter cs1 order ) (scatter cs0 order ), scheme(lean2) xlabel(1 "nonfiction"	2 "fiction" 3 "poetry"	4 "history"	5 "comics"	6 "children"	7 "mystery"	8 "fantasy"	9 "young adult"	10 "romance", labsize(small) angle(forty_five) noticks)	legend(order(2 "heavy users" 1 "light users")) ytitle("% change in CS") xtitle(genre)

graph export replication\latex_text\figures\delta_cs_by_genre_type_corrected.pdf, as(pdf) name("Graph") replace 
graph export replication\latex_text\delta_cs_by_genre_type_corrected.eps, as(eps) name("Graph") preview(off) replace 

